#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <Windows.h>
#define SIZE 10

HANDLE Start,GameSence01,GameSence02;
COORD coord = { 0,0 };
DWORD bytes = 0;
char user = 0;
struct Position
{
    int x;
    int y;
};
struct Position myPosition;

void PreLoading();
void FreshMap();
void FindSelf();
void MoveLeft();
void MoveRight();
void CreatBullet();
void Bullet();


int data[10][10] =
{
	{1,1,1,1,1,1,1,1,1,1},
	{1,0,0,0,3,0,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,2,0,0,0,0,1},
	{1,1,1,1,1,1,1,1,1,1}
};
char map[SIZE][SIZE] =
{
    {"*********"},
    {"*********"},
    {"*********"},
    {"*********"},
    {"*********"},
    {"*********"},
    {"*********"},
    {"*********"},
    {"*********"},
    {"*********"}
};
int main()
{
    PreLoading();
}
void PreLoading()
{
    Start = CreateConsoleScreenBuffer(
        GENERIC_WRITE,
        FILE_SHARE_WRITE,
        NULL,
        CONSOLE_TEXTMODE_BUFFER,
        NULL
    );
    GameSence01 = CreateConsoleScreenBuffer(
        GENERIC_WRITE,
        FILE_SHARE_WRITE,
        NULL,
        CONSOLE_TEXTMODE_BUFFER,
        NULL
    );
    GameSence02 = CreateConsoleScreenBuffer(
        GENERIC_WRITE,
        FILE_SHARE_WRITE,
        NULL,
        CONSOLE_TEXTMODE_BUFFER,
        NULL
    );
    CONSOLE_CURSOR_INFO cci;
    cci.bVisible = 0;
    cci.dwSize = 1;
    SetConsoleCursorInfo(Start, &cci);
    SetConsoleCursorInfo(GameSence01, &cci);
    SetConsoleCursorInfo(GameSence02, &cci);

    char start[4][10] =
    {
        {"**********"},
        {"* 1.play *"},
        {"* 2.exit *"},
        {"**********"}
    };
    for (int i = 0; i < 4; i++)
    {
        coord.Y = i;
        WriteConsoleOutputCharacterA(Start, start[i], 10, coord, &bytes);
    }
    SetConsoleActiveScreenBuffer(Start);
    user = _getch();
    if (user == '1')
    {
        FreshMap();
        for (int i = 0; i < SIZE; i++)
        {
            coord.Y = i;
            WriteConsoleOutputCharacterA(GameSence01, map[i], 10, coord, &bytes);
        }
        SetConsoleActiveScreenBuffer(GameSence01);
        int count = 0;
        while (1)
        {
            count = !count;
            Sleep(100);
            if (_kbhit())
            {
                user = _getch();
                if (user=='a')          MoveLeft();
                else if (user == 'd')   MoveRight();
            }
            FreshMap();
            for (int i = 0; i < SIZE; i++)
            {
                coord.Y = i;
                if (count)
                    WriteConsoleOutputCharacterA(GameSence02, map[i], 10, coord, &bytes);
                else
                    WriteConsoleOutputCharacterA(GameSence01, map[i], 10, coord, &bytes);
            }
            SetConsoleActiveScreenBuffer(count?GameSence02:GameSence01);
        }
    }
    else exit(0);
}
void FreshMap()
{
    for (int r = 0; r < SIZE; r++)
    {
        for (int c = 0; c < SIZE; c++)
        {
            if (data[r][c]==0)           map[r][c] = ' ';
            else if (data[r][c] == 1)    map[r][c] = '*';
            else if (data[r][c] == 2)    map[r][c] = '^';
            else if (data[r][c] == 3)    map[r][c] = 'V';
            else if (data[r][c] == 4)    map[r][c] = '.';
        }
    }
}
void MoveRight()
{
    FindSelf();
    if (myPosition.y < 8)
    {
        data[myPosition.x][myPosition.y] = 0;
        data[myPosition.x][myPosition.y+1] = 2;
        FindSelf();
    }
}
void MoveLeft()
{
    FindSelf();
    if (myPosition.y > 2)
    {
        data[myPosition.x][myPosition.y] = 0;
        data[myPosition.x][myPosition.y - 1] = 2;
        FindSelf();
    }
}
void FindSelf()
{
    for (int r = 0;r < SIZE;r++)
    {
        for (int c = 0;c < SIZE;c++)
        {
            if (data[r][c] == 2)
            {
                myPosition.x = r;
                myPosition.y = c;
                break;
            }
        }
    }
}
void Bullet(int r,int c)
{
    if (data[r][c]==4 && r>0)
    {
        data[r][c] = 0;
        data[r - 1][c] = 4;
    }
}
void CreatBullet()
{
    //Sleep(0);
    data[myPosition.x - 1][myPosition.y] = 4;
}